package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import model.RegisterSMS;
import model.Teaching_schedule;

public class RegisterSMSMod {
	Connection connection;
	public static RegisterSMSMod isntanceRSMSM;
	public static RegisterSMSMod getIsntance(){
		if(isntanceRSMSM == null){
			isntanceRSMSM = new RegisterSMSMod();
		}
		return isntanceRSMSM;
	}
	public boolean add(RegisterSMS ts){
		boolean result = false;
		PreparedStatement sqlu =null;
		try {
			connection = DB_Connection.getIsntance().getConnection();
			String sql ="INSERT INTO register_sms(pid, scholastic, half) VALUES (? , ? , ?) LIMIT = 1 ";
			sqlu = connection.prepareStatement(sql);
			sqlu.setInt(1, ts.getPid());
			sqlu.setString(2, ts.getScholastic());
			sqlu.setInt(3,ts.getHalf());
			int n = sqlu.executeUpdate();
			if(n > 0) result = true;
			sqlu.close();
			connection.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}
	public RegisterSMS getRegisterSMS(int pid, String scholastic, int half){
		RegisterSMS result = null;
		PreparedStatement sqls = null;
		try {
			connection = DB_Connection.getIsntance().getConnection();
			String sql = "SELECT * FROM register_sms WHERE pid = ? AND scholastic = ? AND half = ? ";
			sqls = connection.prepareStatement(sql);
			sqls.setInt(1, pid);
			sqls.setString(2, scholastic);
			sqls.setInt(3, half);
			ResultSet rs = sqls.executeQuery();
			while (rs.next()) {
				result = new RegisterSMS(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getInt(4));
			}
			sqls.close();
			connection.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}
	public ArrayList<RegisterSMS> getAllRegisterSMS(String scholastic, int half){
		ArrayList<RegisterSMS> result = null;
		PreparedStatement sqls = null;
		try {
			connection = DB_Connection.getIsntance().getConnection();
			String sql = "SELECT * FROM register_sms WHERE scholastic = ? AND half = ? ";
			sqls = connection.prepareStatement(sql);
			sqls.setString(1, scholastic);
			sqls.setInt(2, half);
			ResultSet rs = sqls.executeQuery();
			while (rs.next()) {
				result.add(new RegisterSMS(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getInt(4)));
			}
			sqls.close();
			connection.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}
	public ArrayList<RegisterSMS> getAll(){
		ArrayList<RegisterSMS> result = null;
		PreparedStatement sqls = null;
		try {
			connection = DB_Connection.getIsntance().getConnection();
			String sql = "SELECT * FROM register_sms  ";
			sqls = connection.prepareStatement(sql);
			ResultSet rs = sqls.executeQuery();
			while (rs.next()) {
				result.add(new RegisterSMS(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getInt(4)));
			}
			sqls.close();
			connection.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}
	public boolean del(int rsid){
		PreparedStatement delpd = null;
		boolean result = false;
		Connection connection = DB_Connection.getIsntance().getConnection();
		try {
			
			delpd = connection.prepareStatement("DELETE FROM register_sms WHERE rsid = ? LIMIT 1");
			delpd.setInt(1, rsid);
			int number = delpd.executeUpdate();
			if (number > 0) {
				result = true;
			}
			delpd.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return  result;
	}
	public boolean delByPid(int pid){
		PreparedStatement delpd = null;
		boolean result = false;
		Connection connection = DB_Connection.getIsntance().getConnection();
		try {
			
			delpd = connection.prepareStatement("DELETE FROM register_sms WHERE pid = ? LIMIT 1");
			delpd.setInt(1, pid);
			int number = delpd.executeUpdate();
			if (number > 0) {
				result = true;
			}
			delpd.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return  result;
	}
}
